package com.learn.algorlthm;

/**
 * @classname:
 * @Description:数值的整数次方
 * @author: yfpu(fengpu.yang @ ucarinc.com)
 * @Date: 2018-07-11 16:39
 * 题目描述：
 *  给定一个double类型的浮点数base和int类型的整数exponet。求base的exponent次方
 */
public class Learn16 {

    public double power(double base, int exponent) {
        if (exponent == 0)
            return 1;
        if (exponent == 1)
            return base;
        boolean isNegative = false;
        if (exponent < 0)
            isNegative = true;
            exponent = -exponent;
        double power = power(base, exponent/2);
        if (exponent%2 != 0)
            power = power * base;
        return isNegative ? 1/power : power;

    }
}
